//
//  ArMenuScrollView.m
//  LZMusicPlayer
//
//  Created by wulanzhou on 2016/11/8.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "ArMenuScrollView.h"

#define kArMenuItemWidth   60
#define kArMenuItemHeight  44

@interface ArMenuScrollView () {

    NSArray *_menuTitles;
    NSInteger _currentIndex;
    
    void (^ArMenuSelectedFinshedBlock) (NSInteger index,NSString *title);
}
@property (nonatomic,strong) UIScrollView *menuScrollView;
@end


@implementation ArMenuScrollView

@synthesize selectedIndex = _currentIndex;

- (instancetype)initWithFrame:(CGRect)frame {

    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.menuScrollView];
        _currentIndex = 0;
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles{

    if (self = [super initWithFrame:frame]) {
        [self addSubview:self.menuScrollView];
        _currentIndex = 0;
        _menuTitles = titles;
        
        [self createMenuButtons];
    }
    return self;
}

- (void)setSelectedItemWithIndex:(NSInteger)index {

    if (_currentIndex != index && index < [_menuTitles count]) {
        
        UIButton *btn = [self.menuScrollView viewWithTag:100 + index];
        btn.selected = YES;
        
        UIButton *orginBtn = [self.menuScrollView viewWithTag:100 + _currentIndex];
        orginBtn.selected = NO;
        
        _currentIndex = index;
        
        NSString *title = nil;
        if (_menuTitles&&[_menuTitles count]>0&&_currentIndex<[_menuTitles count]) {
            title = _menuTitles[_currentIndex];
        }
        
        if (ArMenuSelectedFinshedBlock) {
            ArMenuSelectedFinshedBlock(_currentIndex,title);
        }
    }
}

- (void)setSelectedItemCompletion:(void (^) (NSInteger index,NSString *title))completion{
    ArMenuSelectedFinshedBlock = completion;
}


- (UIScrollView *)menuScrollView{

    if (!_menuScrollView) {
        _menuScrollView = [[UIScrollView alloc] initWithFrame:self.bounds];
        _menuScrollView.pagingEnabled = YES;
        _menuScrollView.showsHorizontalScrollIndicator = NO;
        _menuScrollView.backgroundColor = [UIColor colorFromHexRGB:@"f4f4f4"];
    }
    return _menuScrollView;
}

- (void)createMenuButtons{

    if (_menuTitles && [_menuTitles count] > 0) {
        
        [_menuTitles enumerateObjectsUsingBlock:^(NSString *title, NSUInteger index, BOOL * _Nonnull stop) {
            
            UIButton *btn = [self createWithFrame:CGRectMake(kArMenuItemWidth * index, 0, kArMenuItemWidth, kArMenuItemHeight) title:title tag:100+index normalColor:[UIColor colorFromHexRGB:@"7d7d7d"] selectedColor:[UIColor colorFromHexRGB:@"3be6d9"]];
            
            [self.menuScrollView addSubview:btn];
            
        }];
        
        _menuScrollView.contentSize = CGSizeMake([_menuTitles count] * kArMenuItemWidth, self.height);
    }
}

- (UIButton *)createWithFrame:(CGRect)frame title:(NSString *)title tag:(NSInteger)tag normalColor:(UIColor *)normalColor selectedColor:(UIColor *)selectedColor{

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = frame;
    btn.tag = tag;
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setTitleColor:normalColor forState:UIControlStateNormal];
    [btn setTitleColor:selectedColor forState:UIControlStateSelected];
    [btn addTarget:self action:@selector(menuItemClick:) forControlEvents:UIControlEventTouchUpInside];
    btn.titleLabel.font = [UIFont systemFontOfSize:14.0f];
    if (_currentIndex == tag - 100) {
        btn.selected = YES;
    }else{
        btn.selected = NO;
    }
    //btn.backgroundColor = [UIColor randomColor];
    return btn;
}

- (void)menuItemClick:(UIButton *)sender{
    [self setSelectedItemWithIndex:sender.tag - 100];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
